package com.common.util;

import java.lang.reflect.Field;
import java.util.Map;
import java.util.Random;

import org.apache.log4j.Logger;

import com.common.vo.Result;


public class MyXssUtil {
    public static int limit = 20;
    public static int page = 1;
    private static Logger logger = Logger.getLogger(MyXssUtil.class);//记录日志

    
    public static String getPath() {
        String path = System.getProperty("catalina.home").replace("bin", "") + "/webapps/Layui-SSM";
        return path;
    }

    public static int RandomInt() {
        Random random = new Random();
        int s = random.nextInt(10000);
        return s;
    }

    public static int RandomInt10() {
        Random random = new Random();
        int s = random.nextInt(10);
        return s;
    }

    /**
     * 新增修改判断，防止XSS攻击
     *
     * @param map
     * @return
     */
    public static Result<?> judgeColumnXSS(Map<String, Object[]> map, Object obj) {
        try {
            if (map != null) {
                for (String key : map.keySet()) {
                    if (map.get(key) != null) {
                        String s = "" + map.get(key)[0];
                        if (s.indexOf("|") != -1 || s.indexOf("&") != -1 || s.indexOf("$") != -1 || s.indexOf("%") != -1
                                || s.indexOf("'") != -1 || s.indexOf("<") != -1 || s.indexOf(">") != -1
                                || s.indexOf("script") != -1 || s.indexOf("document") != -1 || s.indexOf("eval(") != -1) {
          
                            return Result.fail("存在有特殊字符/特殊单词");
                        }
                    }
                }
            }
            if (obj != null) {
                Class<?> aClass = obj.getClass();
                for (; aClass != Object.class; aClass = aClass.getSuperclass()) {
                    Field[] fields = aClass.getDeclaredFields();
                    for (Field field : fields) {
                        field.setAccessible(true);
                        String s = "" + field.get(obj);
                        if (s.indexOf("|") != -1 || s.indexOf("&") != -1 || s.indexOf("$") != -1 || s.indexOf("%") != -1
                                || s.indexOf("'") != -1 || s.indexOf("<") != -1 || s.indexOf(">") != -1
                                || s.indexOf("script") != -1 || s.indexOf("document") != -1 || s.indexOf("eval(") != -1) {
                        	return Result.fail("存在有特殊字符/特殊单词");
                        }
                    }
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
            return Result.fail("转义符/编码，判断异常");
        }
        return Result.success("无转义符/编码");
    }
}
